// 运费模板
import { getTransport, transportDelete, transportSearch } from '@/services';
import { IShopTransport } from '@/types';
import { Effect, Reducer } from 'umi';

export interface TransPortModelState {
  TransPortList:IShopTransport[],
  TransPortListLength:number
}

export interface TransPortModelType {
  namespace: 'shopTransport';
  state: TransPortModelState;
  effects: {
    getTransport: Effect;
    transportDelete:Effect;
    transportSearch:Effect
  };
  reducers: {
    save: Reducer<TransPortModelState>;
  };
}

const TransPortModel: TransPortModelType = {
  namespace: 'shopTransport',

  state: {
    TransPortList:[],
    TransPortListLength:0
  },

  effects: {
    // 查询
    *getTransport({ payload }, { call, put }) {
      let result = yield call(getTransport,payload);
      console.log('result......',result);
      if(result){
        yield put({
          type:"save",
          payload:{
            TransPortList:result.records.map((item:any)=>{
              item.key = item.transportId;
              return item;
            })
          }
        })
      }
    },
    // 删除
    *transportDelete({payload},{call,put}){
        let result  = yield call(transportDelete,payload);
        console.log("del.....",result);
     
          let res = yield call(getTransport,1);
          console.log('result......',res);
          if(res){
            yield put({
              type:"save",
              payload:{
                TransPortList:res.records.map((item:any)=>{
                  item.key = item.transportId;
                  return item;
                })
              }
            })
          }   
    },
    *transportSearch({payload},{call,put}){
        let {transName} = payload
        let result  = yield call(transportSearch,transName);
        console.log("search result",result);
        
        if(result){
          yield put({
            type:"save",
            payload:{
              TransPortList:result.records.map((item:any)=>{
                item.key = item.transportId;
                return item
              })
            }
          })
        }
    }
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    
  }
  
};

export default TransPortModel;